﻿
using System;
using System.Windows;
using System.Windows.Controls;
using MyRemoteDesktop.Core;
using MyRemoteDesktop.Modes;

namespace MyRemoteDesktop.Container
{
    /// <summary>
    /// RdpPage.xaml 的交互逻辑
    /// </summary>
    public partial class Home_Setting : UserControl
    {
        public Home_Setting()
        {
            InitializeComponent();

            if (DatabaseServices.IsPassword(null)) PART_Psw_Old.IsEnabled = false;
            General_ComboBox_Location.SelectedIndex = (int)UserSettings.AppDataLocation;
            General_ComboBox_Location.SelectionChanged += General_ComboBox_Location_SelectionChanged;
            TextBox_PWD_TIP.Text = UserSettings.PasswordTip;
            TextBox_PWD_TIP.TextChanged += TextBox_PWD_TIP_Changed;

            Bak_LIST.ItemsSource = UserSettings.getBakList();
        }

        private void PART_Psw_Change_Click(object sender, RoutedEventArgs e)
        {
            if (PART_Psw_Old.IsEnabled && !DatabaseServices.IsPassword(PART_Psw_Old.Password))
            {
                MainWindow.ShowNoticeDialog("提示", "修改密码失败，原密码不正确。");
                return;
            }
            else if (PART_Psw_New.Password != PART_Psw_Repeat.Password)
            {
                MainWindow.ShowNoticeDialog("提示", "修改密码失败，两次输入的密码不一致。");
                return;
            }
            else if (string.IsNullOrWhiteSpace(TextBox_PWD_TIP.Text))
            {
                MainWindow.ShowNoticeDialog("提示", "请输入密码提示，以便忘记密码时候给予提醒。");
                return;
            }
            DatabaseServices.ChangePassword(PART_Psw_New.Password);
            MainWindow.ShowNoticeDialog("提示", "修改密码成功。");

            PART_Psw_Old.IsEnabled = !DatabaseServices.IsPassword(null);
            PART_Psw_New.Password = "";
            PART_Psw_Old.Password = "";
            PART_Psw_Repeat.Password = "";
        }

        /// <summary>
        /// 数据存储目录被改变
        /// </summary>
        private void General_ComboBox_Location_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UserSettings.NewAppDataLocation = (DataStoreLocation)General_ComboBox_Location.SelectedIndex;
        }

        private void TextBox_PWD_TIP_Changed(object sender, TextChangedEventArgs e)
        {
            UserSettings.PasswordTip = TextBox_PWD_TIP.Text;

        }

        private void BAK_Click(object sender, RoutedEventArgs e)
        {
            MainWindow.ShowMessageDialog("提示", UserSettings.bak());
            Bak_LIST.ItemsSource = UserSettings.getBakList();
        }

        private void RESTORE_Click(object sender, RoutedEventArgs e)
        {
            if(Bak_LIST.SelectedItem!=null&&!string.IsNullOrWhiteSpace(Bak_LIST.SelectedItem.ToString()))
                MainWindow.ShowMessageDialog("提示", UserSettings.resore(Bak_LIST.SelectedItem.ToString()));
            else
                MainWindow.ShowMessageDialog("提示", "恢复文件不能为空！");
        }

    }
}